﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OutlookComponent;
using System.Threading;

namespace QuickMeeting
{
    public partial class MeetingMan : Form
    {
        public MeetingMan()
        {
            InitializeComponent();
            dtpEndTime.Value = dtpStartTime.Value.Add(new TimeSpan(1, 0, 0));
        }

        private void dtpEndTime_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker endTimeControl = sender as DateTimePicker;
            DateTime endTime = endTimeControl.Value;

            DateTime startTime = dtpStartTime.Value;

            tbDuration.Text = Convert.ToInt32(endTime.Subtract(startTime).TotalMinutes).ToString();
        }

        private void tbDuration_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int duration = Convert.ToInt32(tbDuration.Text.Trim());
                TimeSpan durationTS = new TimeSpan(0, duration, 0);
                dtpEndTime.Value = dtpStartTime.Value.Add(durationTS);
            }
            catch (Exception exception)
            {
                tbDuration.Focus();
            }
        }

        private void dtpStartTime_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                int duration = Convert.ToInt32(tbDuration.Text.Trim());
                TimeSpan durationTS = new TimeSpan(0, duration, 0);
                dtpEndTime.Value = dtpStartTime.Value.Add(durationTS);
            }
            catch (Exception exception)
            {
                tbDuration.Focus();
            }
        }

        private void reportABugToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OutlookHelper.CreateMailItem("johnson.ji@thomsonreuters.com", "Report bug of Meeting Man", string.Empty);
        }

        private void giveFeedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OutlookHelper.CreateMailItem("johnson.ji@thomsonreuters.com", "Feedback of Meeting Man", string.Empty);
        }

        private void btnBook_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(BookMeetingRoom, this);

        }


        private void BookMeetingRoom(object state)
        {
            (state as MeetingMan).Invoke(new ShowStatus(ShowLoadingMessge), true);
            DateTime startTime = dtpStartTime.Value;
            DateTime endTime = dtpEndTime.Value;

            try
            {
                if (OutlookHelper.BookMeetingRoom(startTime, endTime, tbSubject.Text, tbBody.Text) == false)
                {
                    (state as MeetingMan).Invoke(new msgHandler(DisplayMessage), "", "There is no available meetingroom at this period time, please try other time.", MessageBoxIcon.Information);
                }
            }
            catch (Exception exp)
            {
                (state as MeetingMan ).Invoke( new msgHandler( DisplayMessage ), "Failed", "There is an error raised on searching, please try again.\n Error Message: " + exp.Message, MessageBoxIcon.Error );
            }
            finally
            {
                (state as MeetingMan).Invoke(new ShowStatus(ShowLoadingMessge), false);
            }
        }

        public delegate void msgHandler(string title, string msg, MessageBoxIcon ico);

        public void DisplayMessage( string title, string msg, MessageBoxIcon icon )
        {
            MessageBox.Show(this, msg, title, MessageBoxButtons.OK, icon);
        }

        public delegate void ShowStatus(bool isLoading);

        public void ShowLoadingMessge(bool isLoading)
        {
            if (isLoading)
            {
                this.statusLabel.Text = "Loading.....";
            }
            else
            {
                this.statusLabel.Text = string.Empty;
            }
        }
    }

 
}
